package com.leetcode.algorithm.y22.m07.w4;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 1630. 等差子数组
 * 
 * https://leetcode.cn/problems/arithmetic-subarrays/
 * 
 * @author jie.deng
 *
 */
class Question1630Solution01 {
	
	public List<Boolean> checkArithmeticSubarrays(int[] nums, int[] l, int[] r) {
		List<Boolean> list = new ArrayList<>();
		for (int i = 0; i < l.length; i++) {
			int left = l[i];
			int right = r[i];
			if (left >= right) {
				list.add(Boolean.FALSE);
			} else if (right == left + 1) {
				list.add(Boolean.TRUE);
			} else {
				int[] arr = Arrays.copyOfRange(nums, left, right + 1);
				Arrays.sort(arr);
				int diff = arr[1] - arr[0];
				Boolean flag = Boolean.TRUE;
				for (int j = 1; j < arr.length - 1; j++) {
					if (diff != arr[j + 1] - arr[j]) {
						flag = Boolean.FALSE;
						break;
					}
				}
				list.add(flag);
			}
		}
		return list;
	}
    
}